<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="请按照关键字搜索"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
        </el-form-item>
        <el-form-item> </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <div class="flex justify-between">
          <div>
            <el-button type="primary" @click="handleCreate()">
              创建收支信息
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="tableList"
        highlight-current-row
        border
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          key="prjNum"
          label="项目编号"
          align="center"
          prop="prjNum"
          width="120"
        />
        <el-table-column
          key="prjName"
          label="项目名称"
          width=""
          align="center"
          prop="prjName"
        />
        <el-table-column
          key="bankSerialNumber"
          label="银行流水号"
          width="100"
          align="center"
          prop="bankSerialNumber"
        >
        </el-table-column>

        <el-table-column
          key="incomeTypeName"
          label="收支类型"
          align="center"
          width="150"
          prop="incomeTypeName"
        >
        </el-table-column>
        <el-table-column
          key="tradeTypeName"
          label="交易类型"
          width="195"
          align="center"
          prop="tradeTypeName"
        >
        </el-table-column>
        <el-table-column
          key="accountName"
          label="账户名称"
          width="140"
          align="center"
          prop="accountName"
        />
        <el-table-column
          key="bankNo"
          label="银行账户"
          width="150"
          align="center"
          prop="bankNo"
        />
        <el-table-column
          key="balance"
          label="账户余额"
          width="150"
          align="center"
          prop="balance"
        />
        <el-table-column
          key="amount"
          label="收支余额"
          width="150"
          align="center"
          prop="amount"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          :width="mobile === 'mobile' ? 110 : 220"
        >
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click="openDetaill(scope.row)"
            >
              <i-ep-grid />
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageIndex"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <addForm
      v-if="dialogAdd.visible"
      class="detail"
      :style="{
        '--mobileAddFormHeight':
          mobile === 'mobile' ? screenHeight - 117 + 'px' : 'auto',
      }"
      :dialog-add="dialogAdd"
      :data="addFormData"
      :select-project="selectProject"
      @send-close="sendClose"
      @send-open-select="sendOpenSelect"
    ></addForm>
    <SelectProject
      v-if="dialogSelect.visible"
      class="detail"
      :style="{
        '--mobileAddFormHeight':
          mobile === 'mobile' ? screenHeight - 117 + 'px' : 'auto',
      }"
      :dialog-select="dialogSelect"
      @send-close="sendClose"
      @confirm-project="confirmProject"
    ></SelectProject>
    <detail-description
      v-if="dialogDetail.visible"
      class="detail"
      :style="{
        '--mobileAddFormHeight':
          mobile === 'mobile' ? screenHeight - 117 + 'px' : 'auto',
      }"
      :dialog-detail="dialogDetail"
      :data="currentItem"
      @send-close="sendClose"
    ></detail-description>
  </div>
</template>

<script lang="ts">
export default {
  name: "Projectspecialaccount",
};
</script>

<script lang="ts" setup>
import { ref, reactive, computed } from "vue";
import { ElTable, ElForm } from "element-plus";
import {
  ParamsType,
  ProjectAccount,
  ProjectSpecialAccountPayment,
  ProjectSpecialAccountPaymentEditForm,
} from "@/api/system/projectspecialaccount/types";
import { getProjectAccountPaymentList } from "@/api/system/projectspecialaccount/index";
import addForm from "@/views/system/projectspecialaccount/projectspecialaccountpayment/addForm.vue";
import DetailDescription from "@/views/system/projectspecialaccount/projectspecialaccountpayment/detail.vue";
import SelectProject from "@/views/system/projectspecialaccount/selectprojectspecialaccount.vue";
import { useScreen } from "@/store/modules/screen";

const screenStore = useScreen();
const mobile = computed(() => screenStore.device);
const screenHeight = computed(() => screenStore.screenHeight);

function initScreen() {}

const tableList = ref<ProjectSpecialAccountPayment[]>();
const total = ref(0);
const loading = ref(false);
const queryFormRef = ref(ElForm);
const queryParams = reactive<ParamsType>({
  pageIndex: 1,
  pageSize: 20,
});

const dialogAdd = reactive<DialogOption>({
  visible: false,
  title: "创建收支明细",
  id: 1,
});

const dialogSelect = reactive<DialogOption>({
  visible: false,
  title: "选择项目",
  id: 2,
});

const dialogDetail = reactive<DialogOption>({
  visible: false,
  title: "查看详情",
  id: 2,
});

const currentItem = ref<ProjectSpecialAccountPayment>();
const addFormData = ref<ProjectSpecialAccountPaymentEditForm>();
const selectProject = ref<ProjectAccount>();

/* 列表查询 */
async function handleQuery() {
  loading.value = true;
  let list = await getProjectAccountPaymentList(queryParams);
  if (list.type === 0) {
    if (list.success) {
      tableList.value = list.data.list;
      queryParams.pageIndex = list.data.pageNum;
      queryParams.pageSize = list.data.pageSize;
      total.value = list.data.total;
    } else {
      ElMessage.error(list.msg);
    }
  } else {
    ElMessage.error(list.msg);
  }
  loading.value = false;
}

/* 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageIndex = 1;
  queryParams.pageSize = 20;
  handleQuery();
}

/* 创建 */
function handleCreate() {
  dialogAdd.visible = true;
}

/* 查看详情 */

function openDetaill(data: ProjectSpecialAccountPayment) {
  if (data) {
    currentItem.value = data;
    dialogDetail.visible = true;
  }
}

/**关闭弹窗 */
function sendClose(dialog: string, query: boolean) {
  if (dialog == "add") {
    addFormData.value = undefined;
    selectProject.value = undefined;
    dialogAdd.visible = false;
  }
  if (dialog == "detail") {
    dialogDetail.visible = false;
  }
  if (dialog == "select") {
    dialogSelect.visible = false;
  }
  if (query) {
    handleQuery();
  }
}
/**打开选择项目弹窗 */
function sendOpenSelect(data: ProjectSpecialAccountPaymentEditForm) {
  addFormData.value = data;
  dialogAdd.visible = false;
  dialogSelect.visible = true;
}
/**确认选择项目 */
function confirmProject(data: ProjectAccount) {
  selectProject.value = data;
  dialogAdd.visible = true;
}

onMounted(() => {
  initScreen();
  handleQuery();
});
</script>
